<template>
    <div class="fare-list">
        <el-card>
            <!-- 添加搜索表单 -->
            <el-form
                ref="formRef"
                :model="queryParams"
                :inline="true"
                class="search-form"
            >
                <el-form-item label="公司名称">
                    <el-select
                        v-model="queryParams.company_id"
                        placeholder="请选择公司"
                        clearable
                        class="w-[200px]"
                    >
                        <el-option
                            v-for="item in companyOptions"
                            :key="item.id"
                            :label="item.company_name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getLists">搜索</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <el-button type="primary" @click="handleAdd">新建运价</el-button>
                </el-form-item>
            </el-form>
            <el-table
                v-loading="pager.loading"
                :data="pager.lists"
                class="mt-4"
                size="large"
            >
                <el-table-column label="运价ID" prop="id" width="100" align="center" />
                <el-table-column label="运价编号" prop="fare_num" width="150" align="center" />
                <el-table-column label="公司名称" prop="company.company_name" min-width="150" align="center" />
                <el-table-column label="运价配置" min-width="300">
                    <template #default="{ row }">
                        <div v-for="(rule, index) in row.mileage_rules" :key="index" class="mb-2">
                            <template v-if="index === row.mileage_rules.length - 1">
                                {{ rule.start }}公里以上，
                                <template v-if="rule.type === 1">
                                    固定价：{{ rule.fixed_price }}元
                                </template>
                                <template v-else>
                                    每公里{{ rule.per_km_price }}元
                                </template>
                            </template>
                            <template v-else>
                                {{ rule.start }}-{{ rule.end }}公里，
                                <template v-if="rule.type === 1">
                                    固定价：{{ rule.fixed_price }}元
                                </template>
                                <template v-else>
                                    每公里{{ rule.per_km_price }}元
                                </template>
                            </template>
                        </div>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="状态" width="120" align="center">
                    <template #default="{ row }">
                        <el-switch
                            v-model="row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="handleStatusChange(row)"
                        />
                    </template>
                </el-table-column> -->
                <el-table-column label="操作" width="180" fixed="right" align="center">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                        <el-button type="danger" link @click="handleDelete(row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>

        <price-config-popup
            v-if="showPriceConfig"
            ref="priceConfigRef"
            @success="getLists"
            @close="showPriceConfig = false"
        />
    </div>
</template>

<script lang="ts" setup name="fare">
import { usePaging } from '@/hooks/usePaging'
import PriceConfigPopup from '../company/price-config.vue'
import feedback from '@/utils/feedback'
import { fareLists,fareDelete } from '@/api/supervise/fare'
import { companyLists } from '@/api/supervise/company'
import type { FormInstance } from 'element-plus'

const formRef = shallowRef<FormInstance>()
const priceConfigRef = shallowRef<InstanceType<typeof PriceConfigPopup>>()
const showPriceConfig = ref(false)

// 查询参数
const queryParams = reactive({
    company_id: undefined
})

// 公司选项
const companyOptions = ref([])

// 获取公司列表
const getCompanyOptions = async () => {
    try {
        const res = await companyLists({})
        companyOptions.value = res.lists || []
    } catch (error) {
        console.error(error)
    }
}

// 使用分页 hook
const { pager, getLists,resetParams } = usePaging({
    fetchFun: fareLists,
    params: queryParams
})

// 编辑运价
const handleEdit = async (row: any) => {
    showPriceConfig.value = true
    await nextTick()
    priceConfigRef.value?.show(row)
}

// 删除运价
const handleDelete = async (row: any) => {
    try {
        await ElMessageBox.confirm('确认删除该运价配置？', '提示', {
            type: 'warning'
        })
        // TODO: 调用删除接口
        await fareDelete({
            'list': [row.id]
        })
        feedback.msgSuccess('删除成功')
        getLists()
    } catch (error) {
        console.error(error)
    }
}

// 修改状态
const handleStatusChange = async (row: any) => {
    try {
        // TODO: 调用修改状态接口
        feedback.msgSuccess('修改成功')
    } catch (error) {
        console.error(error)
    }
}

// 添加运价
const handleAdd = async () => {
    showPriceConfig.value = true
    await nextTick()
    priceConfigRef.value?.show({
        mileage_rules: [
            {
                start: 0,
                end: 3,
                type: 1,
                fixed_price: 10,
                per_km_price: 0
            },
            {
                start: 3,
                end: Infinity,
                type: 2,
                fixed_price: 0,
                per_km_price: 1
            }
        ]
    })
}

onMounted(() => {
    getCompanyOptions()
    getLists()
})
</script>

<style lang="scss" scoped>
.fare-list {
    padding: 20px;

    .search-form {
        margin-bottom: 20px;
    }
}
</style>